package cn.iocoder.yudao.module.game.controller.admin.gameList;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.gameList.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.gameList.GameListDO;
import cn.iocoder.yudao.module.game.service.gameList.GameListService;

@Tag(name = "管理后台 - 游戏列表")
@RestController
@RequestMapping("/game/list")
@Validated
public class GameListController {

    @Resource
    private GameListService listService;

    @PostMapping("/create")
    @Operation(summary = "创建游戏列表")
    @PreAuthorize("@ss.hasPermission('game:list:create')")
    public CommonResult<Long> createList(@Valid @RequestBody GameListSaveReqVO createReqVO) {
        return success(listService.createList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新游戏列表")
    @PreAuthorize("@ss.hasPermission('game:list:update')")
    public CommonResult<Boolean> updateList(@Valid @RequestBody GameListSaveReqVO updateReqVO) {
        listService.updateList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除游戏列表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:list:delete')")
    public CommonResult<Boolean> deleteList(@RequestParam("id") Long id) {
        listService.deleteList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得游戏列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:list:query')")
    public CommonResult<GameListRespVO> getList(@RequestParam("id") Long id) {
        GameListDO list = listService.getList(id);
        return success(BeanUtils.toBean(list, GameListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得游戏列表分页")
    @PreAuthorize("@ss.hasPermission('game:list:query')")
    public CommonResult<PageResult<GameListRespVO>> getListPage(@Valid GameListPageReqVO pageReqVO) {
        PageResult<GameListDO> pageResult = listService.getListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GameListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出游戏列表 Excel")
    @PreAuthorize("@ss.hasPermission('game:list:export')")
    @OperateLog(type = EXPORT)
    public void exportListExcel(@Valid GameListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GameListDO> list = listService.getListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "游戏列表.xls", "数据", GameListRespVO.class,
                        BeanUtils.toBean(list, GameListRespVO.class));
    }

}